package com.fzk.blog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzk.blog.bean.Role;
import com.fzk.blog.bean.vo.RoleInfo;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author fzk
 * @date 2021-08-15 15:46
 */
public interface RoleMapper extends BaseMapper<Role> {
    @Select("SELECT r.name " +
            "   FROM tbl_role r INNER JOIN tbl_user u ON r.id=u.role_id " +
            "   WHERE u.id=#{id} AND u.flag=0 AND r.flag=0 ")
    String getRoleNameByUserId(int id);

    @Select("SELECT r.id,r.name,r.description,COUNT(u.id) AS userNumber " +
            "   FROM tbl_role r LEFT JOIN tbl_user u ON (r.id=u.role_id AND u.flag=0) " +
            "   WHERE r.flag=0 " +
            "   GROUP BY r.id " +
            "   ORDER BY r.id ASC ")
    List<RoleInfo> getRoleInfoList();

    @Select("SELECT r.id,r.name,r.description,COUNT(u.id) AS userNumber " +
            "   FROM tbl_role r LEFT JOIN tbl_user u ON (r.id=u.role_id AND u.flag=0) " +
            "   WHERE r.flag=0 AND r.id=#{id}" +
            "   GROUP BY r.id")
    RoleInfo getRoleInfoById(int id);

    @Select("select * from tbl_role where name=#{name} and flag=0")
    List<Role> selectByName(String name);

    @Update("update tbl_role set flag=1,edit_by=#{loginId} where id=#{roleId} and flag=0")
    int logicalDelete(int roleId, int loginId);
}
